package com.jyh.supermark.service.impl;

import com.jyh.supermark.controller.BuyIndex;
import com.jyh.supermark.dao.impl.UserDaoImpl;
import com.jyh.supermark.domain.User;
import com.jyh.supermark.service.UserService;
import java.util.Scanner;

public class UserServiceImpl implements UserService {
    public static final Scanner INPUT = new Scanner(System.in);
    public static final UserDaoImpl daoImpl = new UserDaoImpl();
    public static final BuyIndex buyIndex = new BuyIndex();
    /**
     * 注册功能
     */
    @Override
    public void register() {
        System.out.println("请输入用户名：");
        String userName = INPUT.next();
        //判断输入的用户名是否为空
        if (userName == null || userName.length() == 0 || userName.isEmpty() == true) {
            System.out.println("用户名不能为空！");
            return;
        }
        //判断用户名是否已经存在
        User userList = daoImpl.find(userName);
        if (userList != null) {
            System.out.println("用户已经存在！");
            return;
        }
        outer:
        while (true) {
            System.out.println("请输入密码：");
            String password = INPUT.next();
            while (password.length() < 6) {
                System.out.println("密码长度不能小于6！");
                System.out.println("请重新输入：");
                password = INPUT.next();
            }
            System.out.println("请再次输入密码,确认密码：");
            String password2 = INPUT.next();
            if (!(password.equals(password2))) {
                System.out.println("两次输入的密码不一致！");
                continue outer;
            } else {
                System.out.println("请输入年龄：");
                Integer age = INPUT.nextInt();
                while (age < 0 || age > 100) {
                    System.out.println("您的年龄必须是0-100之间的数字！");
                    System.out.println("请重新输入年龄：");
                    age = INPUT.nextInt();
                }
                System.out.println("请输入昵称：");
                String otherName = INPUT.next();
                User user = new User(userName, password, otherName, age);
                int count = daoImpl.register(user);
                if (count > 0) {
                    System.out.println("注册成功！");
                }
            }
            return;
        }
    }

    /**
     * 登录功能
     */
    public void login() {
        User user1 = null;
        while (true) {
            System.out.println("请输入用户名：");
            String name = INPUT.next();
            if (name == null || name.length() == 0 || name.isEmpty() == true) {
                System.out.println("用户名不能为空！");
                continue;
            }
            System.out.println("请输入密码：");
            String psd = INPUT.next();

            User login = daoImpl.login(new User(name, psd, null, null));
            if (login == null) {
                System.out.println("用户名或密码错误！");
                return;
            } else {
                System.out.println("登陆成功！");
                user1 = login;
                break;
            }
        }
        buyIndex.buyIndex(user1);
    }
}
